package org.gd.exercise.learnspringbootredis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author name: yushanjiang
 * @version 1.0
 * @description: Redis数据类型-set应用场景
 * @date 2025/9/15 8:35
 */
@RestController
@RequestMapping("/set")
public class SetController {
    @Autowired
    private RedisTemplate redisTemplate;

    /**
    * @description: 微信抽奖小程序-将用户加入抽奖
    * @param: [userId]
    * @return java.lang.String
    * @author name: yushanjiang
    * @date: 2025/9/15 8:56
    */
    @PostMapping("/lottery/adduser/{userId}")
    public String addUser(@PathVariable String userId) {
        String key = "jys_lottery:wx";
        redisTemplate.opsForSet().add(key, userId);
        return "加入抽奖成功，当前抽奖人数为：" + redisTemplate.opsForSet().size(key).toString();
    }

    /**
    * @description: 微信抽奖小程序-抽出count个用户
    * @param: [userId, count]
    * @return java.lang.String
    * @author name: yushanjiang
    * @date: 2025/9/15 8:59
    */
    @PostMapping("lottery/getcountmembers")
    public String getCountMembers(@RequestParam Integer count) {
        String key = "jys_lottery:wx";
        return "中奖用户为：" + redisTemplate.opsForSet().pop(key, count).toString();
    }

    /**
    * @description: 微信抽奖小程序-参与抽奖所有用户
    * @param: []
    * @return java.lang.String
    * @author name: yushanjiang
    * @date: 2025/9/15 9:07
    */
    @GetMapping("lottery/userlist")
    public String getUserList() {
        String key = "jys_lottery:wx";
        return "当前参与抽奖的用户有：" + redisTemplate.opsForSet().members(key).toString();
    }

}
